package service;

import com.alibaba.fastjson.JSONObject;
import dao.*;
import com.alibaba.fastjson.JSONArray;
import pojo.CurriCulum;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 2022.03.17测试完成 由吴俊莹编写完成
 */
public class LogicService {
    //用户登录成功的默认加载课程表页面
    /**
     * 通过登录用户名找到用户id
     * @param userName
     * @return 返回id
     * @throws SQLException
     */
    public int teacherID(String userName) throws SQLException {
        TeacherDao teacherDao=new TeacherDaoImp();
        return teacherDao.teacherID(userName);
    }
    /**
     * 显示当前用户的课表
     * 管理员可查询所有用户排课表
     * @param teacherId
     * @return 返回课程安排
     * @throws SQLException
     */
    public JSONArray UserTable(int teacherId,String sj) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.UserTable(teacherId,sj);
    }

    //用户添加课程安排
    /**
     * 用户添加课程安排
     * @param
     * @return 返回1或0
     */
    public String InsertKe(JSONArray jsonArray,int id) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        ArrayList<String> chong=ChecChong(id,jsonArray);//没课进来查老师同一时间是否有课
        ArrayList<String> cban=curriCulumDao.banji(jsonArray);//查班级同一时间段是否有课
        String message=null;
        String banjie=null;
        if(cban.size()==0){//判断班级同一时间段是否有课
            if(chong.size()==0){//判断老师同一时间是否有课
                int s=curriCulumDao.InsertKe(jsonArray,id);//没课就能新增
                if (s==0) {
                   return "增加失败";
                }
                if (s>=1) {
                   return "增加成功";
                }
            }else {
                    message = String.join(",", chong);
                return "排课时间" +message+ "重复";
            }
        }

            banjie = String.join(",", cban);
            return "班级：" +banjie+ "排课重复";



    }


    //修改用户自己的课程安排
    /**
     * 修改用户自己的课程安排
     * @param curriCulum
     * @return 返回0或1
     */
    public int Updateu(CurriCulum curriCulum,String xx,int yc,String ykm,String yban) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        String[] ks=curriCulum.getClassHours().split(",");
        //把原始的课时转换成数组
        String[] xk=null;
        if(xx.length()>1){
             xk=xx.split(",");
        }

        ArrayList<String> x=curriCulumDao.Upcheck(curriCulum);
        if(x.size()==ks.length){
            int xxx=x.size();
            int xxk=xk.length;
            if(xxx<xxk){
                for(int i=0;i<x.size();i++){
                    for (int v=0;v<xk.length;v++){
                        if(x.get(i).equals(xk[v])){
                            return curriCulumDao.UpdateUes(curriCulum);
                        }
                    }
                }
            }

            if(yc!=curriCulum.getMark()){
                return curriCulumDao.UpdateUes(curriCulum);
            }
            if(!curriCulum.getCourseName().equals(ykm)){
                return curriCulumDao.UpdateUes(curriCulum);

            }
            if(!curriCulum.getClassID().equals(yban)){
                return curriCulumDao.UpdateUes(curriCulum);
            }
          return -2;
        }

        return curriCulumDao.UpdateUes(curriCulum);



    }

    //删除用户自己的课程安排
    /**
     * 删除用户自己的课程安排
     * @param curriCulum
     * @return 返回0或1
     */
    public int DelUesr_Up(CurriCulum curriCulum) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.DelUesr_Up(curriCulum);
    }

    /**
     * 打卡已上课程
     * @param curriCulum
     * @return 返回1或0
     */
    public int ClockIn(CurriCulum curriCulum,String xx) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.ClockIn(curriCulum,xx);
    }

    /**
     * 管理员给用户设置权限
     * @param teacherId
     * @return
     */
    public int Seture(int teacherId,int level) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.SetUser(teacherId,level);
    }

    /**
     * 管理员查看用户总课时
     * @param teacherId
     * @return 返回总课时
     */
    public int CheckUser(int teacherId) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.CheckUser(teacherId);
    }

    /**
     * 查看课时表数据
     * @return time
     */
    public JSONArray ChecHou() throws SQLException {
        TimeDao times=new TimeDaoImp();
        return times.ChaTime();
    }

    /**
     * 显示班级表数据
     * @return
     */
    public JSONArray ShowClass() throws SQLException {
        ClasstableDao clss=new ClasstableDaoImp();
        return clss.ShowClass();
    }

    /**
     * 用户新增课程时查重
     * @return
     */
    public ArrayList<String> ChecChong(int id,JSONArray jsonArray) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        return curriCulumDao.ChecChong(id,jsonArray);
    }

    /**
     * 管理员查看
     */
    public JSONObject selectAll(String classes, String teaName, String date_One, String date_Two, int all, int splitPag, int limitPag) throws SQLException {
        CurriCulumDao curriCulumDao=new CurriCulumDaoImp();
        //获取总数量
        int zzong=curriCulumDao.zong(classes,teaName,date_One,date_Two,all);
        //获取总页数
        int zonye=zzong%limitPag==0?zzong/limitPag:(zzong/limitPag)+1;
        if(splitPag!=0){
            //获取当前页数应该显示那几行数据
            splitPag=(splitPag*limitPag)-limitPag;
        }

        JSONArray jsonArray=curriCulumDao.selectAll(classes,teaName,date_One,date_Two,all,splitPag,limitPag);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("zong",zonye);
        jsonObject.put("ja",jsonArray);
        return jsonObject;
    }
}
